import React from 'react';
import {Icon} from 'antd';
import {isUrl} from '../utils/utils';

const _ = require('lodash');
const menuData = [
  {
    name: (
      <div>
        <Icon type="desktop"/>
        <span style={{fontWeight: 600}}>工作台</span>
      </div>
    ),
    title: '工作台',
    path: 'desktop',
    authority: ['admin', 'user', 'slave']
  },

  {
    name: (
      <div>
        <Icon type="appstore"/>
        <span>任务管理</span>
      </div>
    ),
    title: '任务管理',
    path: 'work',
    authority: ['admin', 'user'],
    children: [
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>任务列表</span>
          </div>
        ),
        title: '任务列表',
        path: 'index',
        authority: ['admin', 'user']
      }]
  },
  {
    name: (
      <div>
        <Icon type="appstore"/>
        <span>采购管理</span>
      </div>
    ),
    title: '采购管理',
    path: 'procure',
    authority: ['admin', 'user'],
    children: [
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>供应商列表</span>
          </div>
        ),
        title: '供应商列表',
        path: 'vendor',
        authority: ['admin', 'user']
      },
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>采购单列表</span>
          </div>
        ),
        title: '采购单列表',
        path: 'index',
        authority: ['admin', 'user']
      },
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>采购退货管理</span>
          </div>
        ),
        title: '采购退货管理',
        path: 'returnOfGoods',
        authority: ['admin', 'user']
      },
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>采购租用管理</span>
          </div>
        ),
        title: '采购租用管理',
        path: 'rent',
        authority: ['admin', 'user']
      },
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>采购验收</span>
          </div>
        ),
        title: '采购验收',
        path: 'acceptance',
        authority: ['admin', 'user']
      },
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>采购单审核</span>
          </div>
        ),
        title: '采购单审核',
        path: 'review',
        authority: ['admin', 'user']
      },
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>采购统计</span>
          </div>
        ),
        title: '采购统计',
        path: 'assess',
        authority: ['admin', 'user']
      },
      // {
      //   name: (
      //     <div>
      //       <Icon type="sliders"/>
      //       <span>供应商统计</span>
      //     </div>
      //   ),
      //   title: '供应商统计',
      //   path: 'vendorAssess',
      //   authority: ['admin', 'user']
      // },
    ]
  },
  {
    name: (
      <div>
        <Icon type="appstore"/>
        <span>订单管理</span>
      </div>
    ),
    title: '订单管理',
    path: 'sales',
    authority: ['admin', 'user'],
    children: [
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>客户列表</span>
          </div>
        ),
        title: '客户列表',
        path: 'customer',
        authority: ['admin', 'user']
      },
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>订单列表</span>
          </div>
        ),
        title: '订单列表',
        path: 'index',
        authority: ['admin', 'user']
      },
      // {
      //   name: (
      //     <div>
      //       <Icon type="sliders"/>
      //       <span>项目管理</span>
      //     </div>
      //   ),
      //   title: '项目管理',
      //   path: 'project',
      //   authority: ['admin', 'user']
      // },
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>商品列表</span>
          </div>
        ),
        title: '商品列表',
        path: 'prod',
        authority: ['admin', 'user']
      },
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>出租管理</span>
          </div>
        ),
        title: '出租管理',
        path: 'rent',
        authority: ['admin', 'user']
      },
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>订单退货管理</span>
          </div>
        ),
        title: '订单退货管理',
        path: 'returnOfGoods',
        authority: ['admin', 'user']
      },
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>订单审核</span>
          </div>
        ),
        title: '订单审核',
        path: 'review',
        authority: ['admin', 'user']
      },
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>订单统计</span>
          </div>
        ),
        title: '订单统计',
        path: 'statistics',
        authority: ['admin', 'user']
      }
    ]
  },
  {
    name: (
      <div>
        <Icon type="appstore"/>
        <span>库存管理</span>
      </div>
    ),
    title: '库存管理',
    path: 'store',
    authority: ['admin', 'user'],
    children: [
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>物料列表</span>
          </div>
        ),
        title: '物料列表',
        path: 'material',
        authority: ['admin', 'user']
      },

      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>入库单列表</span>
          </div>
        ),
        title: '入库单列表',
        path: 'inbound',
        authority: ['admin', 'user']
      },
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>出库单列表</span>
          </div>
        ),
        title: '出库单列表',
        path: 'outbound',
        authority: ['admin', 'user']
      },
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>库存批次列表</span>
          </div>
        ),
        title: '库存批次列表',
        path: 'lots',
        authority: ['admin', 'user']
      },
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>报废管理</span>
          </div>
        ),
        title: '报废管理',
        path: 'discard',
        authority: ['admin', 'user']
      },
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>报废审核</span>
          </div>
        ),
        title: '报废审核',
        path: 'discardReview',
        authority: ['admin', 'user']
      },
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>库存统计</span>
          </div>
        ),
        title: '库存统计',
        path: 'index',
        authority: ['admin', 'user']
      },
    ]
  },
  {
    name: (
      <div>
        <Icon type="appstore"/>
        <span>财务管理</span>
      </div>
    ),
    title: '财务管理',
    path: 'finance',
    authority: ['admin', 'user'],
    children: [
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>账户管理</span>
          </div>
        ),
        title: '账户管理',
        path: 'accountMgr',
        authority: ['admin', 'user']
      },
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>开票管理</span>
          </div>
        ),
        title: '开票管理',
        path: 'invoicing',
        authority: ['admin', 'user']
      },
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>收票管理</span>
          </div>
        ),
        title: '收票管理',
        path: 'invoicingReceipt',
        authority: ['admin', 'user']
      },
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>付款申请表</span>
          </div>
        ),
        title: '付款申请表',
        path: 'payApply',
        authority: ['admin', 'user']
      },
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>付款审核表</span>
          </div>
        ),
        title: '付款申请审核表',
        path: 'payReview',
        authority: ['admin', 'user']
      },

      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>收入记录表</span>
          </div>
        ),
        title: '收入记录表',
        path: 'receipt',
        authority: ['admin', 'user']
      },
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>资产统计</span>
          </div>
        ),
        title: '资产统计',//账户结余总结余、应付应收总的和供应商和客户，
        path: 'assess',
        authority: ['admin', 'user']
      },

    ]
  },
  {
    name: (
      <div>
        <Icon type="appstore"/>
        <span>人事管理</span>
      </div>
    ),
    title: '人事管理',
    path: 'personnel',
    authority: ['admin', 'user'],
    children: [
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>员工列表</span>
          </div>
        ),
        title: '员工列表',
        path: 'index',
        authority: ['admin', 'user']
      },
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>人事表单</span>
          </div>
        ),
        title: '人事表单',
        path: 'tree',
        authority: ['admin', 'user']
      },
      // {
      //   name: (
      //     <div>
      //       <Icon type="sliders"/>
      //       <span>表单管理</span>
      //     </div>
      //   ),
      //   title: '表单管理',
      //   path: 'pic',
      //   authority: ['admin', 'user']
      // },
      // {
      //   name: (
      //     <div>
      //       <Icon type="sliders"/>
      //       <span>人事档案</span>
      //     </div>
      //   ),
      //   title: '人事档案',
      //   path: 'dict',
      //   authority: ['admin', 'user']
      // }

    ]
  },
  {
    name: (
      <div>
        <Icon type="appstore"/>
        <span>销售管理</span>
      </div>
    ),
    title: '销售管理',
    path: 'salesMgr',
    authority: ['admin', 'user'],
    children: [
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>业务员投标记录跟踪表</span>
          </div>
        ),
        title: '业务员投标记录跟踪表',
        path: 'bid',
        authority: ['admin', 'user']
      }
      , {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>客户开发跟踪表</span>
          </div>
        ),
        title: '客户开发跟踪表',
        path: 'customer',
        authority: ['admin', 'user']
      }
      , {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>供应商平台账户密码</span>
          </div>
        ),
        title: '供应商平台账户密码',
        path: 'account',
        authority: ['admin', 'user']
      }
    ]
  },
  {
    name: (
      <div>
        <Icon type="appstore"/>
        <span>实验管理</span>
      </div>
    ),
    title: '实验管理',
    path: 'experiment',
    authority: ['admin', 'user'],
    children: [
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>实验登记</span>
          </div>
        ),
        title: '实验登记',
        path: 'list',
        authority: ['admin', 'user']
      },
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>寄样登记</span>
          </div>
        ),
        title: '寄样登记',
        path: 'simple',
        authority: ['admin', 'user']
      },
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>实验消耗</span>
          </div>
        ),
        title: '实验消耗',
        path: 'store',
        authority: ['admin', 'user']
      },
      // {
      //   name: (
      //     <div>
      //       <Icon type="sliders"/>
      //       <span>每日填报</span>
      //     </div>
      //   ),
      //   title: '每日填报',
      //   path: 'day',
      //   authority: ['admin', 'user']
      // }
    ]
  },
  // {
  //   name: (
  //     <div>
  //       <Icon type="appstore"/>
  //       <span>每日填报</span>
  //     </div>
  //   ),
  //   title: '每日填报',
  //   path: 'day',
  //   authority: ['admin', 'user'],
  //   children: [
  //     {
  //       name: (
  //         <div>
  //           <Icon type="sliders"/>
  //           <span>人事管理</span>
  //         </div>
  //       ),
  //       title: '人事管理',
  //       path: 'personnel',
  //       authority: ['admin', 'user'],
  //     },
  //     {
  //       name: (
  //         <div>
  //           <Icon type="sliders"/>
  //           <span>实验管理</span>
  //         </div>
  //       ),
  //       title: '实验管理',
  //       path: 'experiment',
  //       authority: ['admin', 'user'],
  //     },
  //   ],
  // },


  {
    name: (
      <div>
        <Icon type="appstore"/>
        <span>行政管理</span>
      </div>
    ),
    title: '行政管理',
    path: 'dic',
    authority: ['admin', 'user']
    // children: [
    // {
    //   name: (
    //     <div>
    //       <Icon type="sliders"/>
    //       <span>每日填报</span>
    //     </div>
    //   ),
    //   title: '每日填报',
    //   path: 'pic',
    //   authority: ['admin', 'user'],
    // },
    // {
    //   name: (
    //     <div>
    //       <Icon type="sliders"/>
    //       <span>寄样品登记表</span>
    //     </div>
    //   ),
    //   title: '寄样品登记表',
    //   path: 'sample',
    //   authority: ['admin', 'user'],
    // },{
    //   name: (
    //     <div>
    //       <Icon type="sliders"/>
    //       <span>实验登记表</span>
    //     </div>
    //   ),
    //   title: '实验登记表',
    //   path: 'experiment',
    //   authority: ['admin', 'user'],
    // },

    // ],
  },
  {
    name: (
      <div>
        <Icon type="appstore"/>
        <span>系统配置</span>
      </div>
    ),
    title: '系统配置',
    path: 'settings',
    authority: ['admin', 'user'],
    children: [
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>字典管理</span>
          </div>
        ),
        title: '字典管理',
        path: 'dict',
        authority: ['admin', 'user']
      },
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>参数配置</span>
          </div>
        ),
        title: '参数配置',
        path: 'index',
        authority: ['admin', 'user']
      },
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>角色管理</span>
          </div>
        ),
        title: '角色管理',
        path: 'role',
        authority: ['admin']
      },
      {
        name: (
          <div>
            <Icon type="sliders"/>
            <span>员工管理</span>
          </div>
        ),
        title: '员工管理',
        path: 'employee',
        authority: ['admin']
      }
      // {
      //   name: (
      //     <div>
      //       <Icon type="sliders"/>
      //       <span>列表表头管理</span>
      //     </div>
      //   ),
      //   title: '列表表头管理',
      //   path: 'tableMgr',
      //   authority: ['admin', 'user'],
      // },
      // {
      //   name: (
      //     <div>
      //       <Icon type="sliders"/>
      //       <span>测试界面</span>
      //     </div>
      //   ),
      //   title: '测试界面',
      //   path: 'test',
      //   authority: ['admin', 'user'],
      // },
    ]
  },

  {
    name: (
      <div>
        <Icon type="sliders"/>
        <span>租户管理</span>
      </div>
    ),
    title: '租户管理',
    path: 'tenant',
    authority: ['su']
  }
];

function formatter(data, parentPath = '/', parentAuthority) {
  // console.log('formatter',data,parentAuthority)
  return data.map(item => {
    let {path} = item;
    if (!isUrl(path)) {
      path = parentPath + item.path;
    }

    const result = {
      ...item,
      path,
      authority: item.authority || parentAuthority
    };
    if (item.children) {
      result.children = formatter(item.children, `${parentPath}${item.path}/`, item.authority);
    }
    return result;
  });
}

function formatter2(data, filter, parentPath = '/', parentAuthority) {
  return data.map(item => {
    let {path} = item;
    if (!isUrl(path)) {
      path = parentPath + item.path;
    }
    //  console.log('includess',path)
    if (_.includes(filter, path)) {
      const result = {
        ...item,
        path,
        authority: item.authority || parentAuthority
      };
      if (item.children) {
        result.children = formatter2(item.children, filter, `${parentPath}${item.path}/`, item.authority);
      }
      return result;
    }
  });
}

function filterMenu(menuData) {
  const loginInfo = getStore('loginInfo');
  const {userType, role, isSlave} = loginInfo;
  /**
   * 超级用户的菜单配置
   */
  if (userType === 'su') {
    const tenant = _.find(menuData, item => item.path === 'tenant');
    const desktop = _.find(menuData, item => item.path === 'desktop');
    const iotSettings = _.find(menuData, item => item.path === 'iotSettings');
    return [tenant, desktop, iotSettings];
  }
  if (userType === 'admin') {
    _.remove(menuData, item => item.path === 'tenant');
    return formatter(menuData);
  }
  if (!role) {
    window.localStorage.clear();
    return null;
  }
  const {menus} = role;
  let temp = formatter2(menuData, menus);
  temp = temp.filter(item => item);
  /**
   * 非自助服务登录时，没有selfService菜单
   */
  if (!isSlave) {
    temp = temp.filter(item => item.path !== '/selfService');
  }
  console.log('menuss', temp);
  return temp;
}

function getStore(name) {
  if (!name) return;
  const content = window.localStorage.getItem(name);
  try {
    return JSON.parse(content);
  } catch {
    return {};
  }
}

export const getMenuData = () => formatter(menuData);
export const getFilterMenuData = () => filterMenu(menuData);
export const getMenu = () => menuData;
